package com.admission.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Notice;
import com.admission.service.NoticeManager;
import com.admission.util.FileUtils;
import com.admission.util.PageUtils;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/base")
public class NoticeAction extends CrudActionSupport<Notice> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6850765952225607163L;

	private Notice entity;
	private NoticeManager noticeManager;
	private Page<Notice> page = new Page<Notice>(PageUtils.DEFAULT_PAGE_SIZE);
	// 搜索的关键字，默认为空
	private String keywords = "";
	//搜索为最新导读
	private String type="";
	private String id;

	public Notice getModel() {
		return entity;
	}
	
	// 异步查询访问人数
			public String visitorCount() throws IOException {
				String path = ServletActionContext.getServletContext().getRealPath(
						"/WEB-INF");
				String r = "";
				path += "/other/visitCount.properties";
				File f = new File(path);
				if (f.exists()) {
					f.createNewFile();
				}
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				if (session.get(id) == null) {
					r = FileUtils.getProperties(path, id, "1", true);
					session.put(id, "1");
				} else {
					r = FileUtils.getProperties(path, id, "1", false);
				}

				Struts2Utils.renderText(r);
				return null;
			}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		
		//如果有关键字输入，则添加模糊搜索条件到filters
		if (keywords != "") {
			PropertyFilter p = new PropertyFilter("LIKES_title", keywords);
			filters.add(p);
		}
		if(type!=""){
			PropertyFilter p1=new PropertyFilter("EQS_latestReading", type);
			filters.add(p1);
		}
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("inputTime");
			page.setOrder(Page.DESC);
		}
		page = noticeManager.searchNotice(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new Notice();
		}else{
			entity = noticeManager.getById(id);
		}
	}

	public Notice getEntity() {
		return entity;
	}

	public void setEntity(Notice entity) {
		this.entity = entity;
	}

	public Page<Notice> getPage() {
		return page;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setNoticeManager(NoticeManager noticeManager) {
		this.noticeManager = noticeManager;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
